package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.CharacterApi;
import com.tanhua.dubbo.api.ResultApi;
import com.tanhua.dubbo.api.TestSoulApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.Character;
import com.tanhua.model.domain.Dimensions;
import com.tanhua.model.domain.Result;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.ResultVo;
import com.tanhua.server.interceptor.RandomById;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ResultService {

    @DubboReference
    private ResultApi resultApi;

    @DubboReference
    private TestSoulApi testSoulApi;

    @DubboReference
    private CharacterApi characterApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    public String saveResult(Map map) {

        List<String> list = new ArrayList();

        ArrayList answers = (ArrayList) map.get("answers");

        for (int i = 0; i < answers.size(); i++) {
            LinkedHashMap o = (LinkedHashMap) answers.get(i);
            Object optionId = o.get("optionId");
            list.add((String) optionId);
        }

        System.out.println(list);

        //获取图片和维度
        List<Character> characters = characterApi.findAll();

        Map<String, String> cover = RandomById.cover(list, characters);

        Long characterId = Long.valueOf(cover.get("characterId"));
        String dimensions = cover.get("dimensions");

        //获取问卷id
        Long tsId = testSoulApi.selectId(list.size());


        // id user_id ts_id result dimensions
        Result result = new Result();
        result.setUserId(UserHolder.getUserId());
        result.setTsId(tsId);
        result.setCharacterId(characterId);
        result.setDimensions(dimensions);

        Result result1 = resultApi.selectByUserId(UserHolder.getUserId());
        Long id = 0l;
        if(result1 == null){
            id = resultApi.save(result);
        }else {
            result.setId(result1.getId());
            id = resultApi.update(result);
        }


        return  id.toString();
    }

    public ResultVo selectResult(Integer id) {

        ResultVo resultVo = new ResultVo();

        Result result = resultApi.selectById(id);

        //性格
        Character character = characterApi.selectById(result.getCharacterId());
        String conclusion = character.getConclusion();
        String cover = character.getCover();

        //维度
        List<Dimensions> dimensionsList = new ArrayList<>();
        String resultDimensions = result.getDimensions();
        Dimensions dimensions =null;

        String[] split = resultDimensions.split(",");
        for (String s : split) {
            String[] split1 = s.split(":");
            dimensions = new Dimensions(split1[0],split1[1]);
            dimensionsList.add(dimensions);
        }


        //查询性格相似的好友
        List<UserInfo> userInfoList = new ArrayList<>();
        List<Result> list = resultApi.selectByIds(result.getTsId(),result.getCharacterId(),UserHolder.getUserId());

        if (list != null && list.size() > 0) {
            List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
            Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);

            for (Result res : list) {
                UserInfo userInfo = map.get(res.getUserId());
                userInfoList.add(userInfo);
            }
        }

        resultVo.setConclusion(conclusion);
        resultVo.setCover(cover);
        resultVo.setDimensions(dimensionsList);
        resultVo.setSimilarYou(userInfoList);

        return resultVo;
    }
}
